#include "Clock.h";

extern LiquidCrystal lcd;

bool new_day;
bool new_hr;
bool new_min;
bool new_sec;

void Clock::init(){
  new_hr = false;
  new_min = false;
  new_sec = false;

  last_day     = RTC.get(DS1307_DOW,true);
  last_hr     = RTC.get(DS1307_HR,false);
  last_min   = RTC.get(DS1307_MIN,false);
  last_sec   = RTC.get(DS1307_SEC,false);
}

void Clock::update_clock(){
  new_day = false;
  new_hr = false;
  new_min = false;
  new_sec = false;

  if ( last_day !=  RTC.get(DS1307_DOW,true) ){
    new_day = true;
  }

  if ( last_hr !=  RTC.get(DS1307_HR,false) ){
    new_hr = true;
  }

  if ( last_min !=  RTC.get(DS1307_MIN,false) ){
    new_min = true;
  }

  if ( last_sec !=  RTC.get(DS1307_SEC,false) ){
    new_sec = true;
  }

  last_day     = RTC.get(DS1307_DOW,false);
  last_hr     = RTC.get(DS1307_HR,false);
  last_min   = RTC.get(DS1307_MIN,false);
  last_sec   = RTC.get(DS1307_SEC,false);
}

void Clock::print_time_min(int hr,int min){
  if ( hr < 10 ){
    lcd.print(" ");
  }
  lcd.print(hr);
  lcd.print(":");
  if ( min < 10 ){
    lcd.print("0");
  }
  lcd.print(min);
}
void Clock::print_time(int hr,int min,int sec){
  print_time_min(hr,min);
  lcd.print(":");
  if ( sec  < 10 ){
    lcd.print("0");
  }
  lcd.print(sec);//read seconds

}

void Clock::print_time_min(){
  if ( RTC.get(DS1307_HR,false) < 10 ){
    lcd.print(" ");
  }
  lcd.print(RTC.get(DS1307_HR,false)); //read the hour 
  lcd.print(":");
  if ( RTC.get(DS1307_MIN,false) < 10 ){
    lcd.print("0");
  }
  lcd.print(RTC.get(DS1307_MIN,false));//read minutes 
}

void Clock::print_time_sec(){
  print_time_min();
  lcd.print(":");
  if ( RTC.get(DS1307_SEC,false)  < 10 ){
    lcd.print("0");
  }
  lcd.print(RTC.get(DS1307_SEC,false));//read seconds
}

void Clock::print_date(int yr ,int  mth ,int date ,int day){
  lcd.print(date);
  lcd.print("/");
  lcd.print(mth);
  lcd.print("/");
  lcd.print(yr);
  lcd.print(" ");
  strcpy_P(buffer, (char*)pgm_read_word(&(  days_names_table[  (day -1 ) ]     )));
  lcd.print(buffer );
}

void Clock::print_date(){
  if ( RTC.get(DS1307_DATE,false) < 10 ){
    lcd.print("0");
  }
  lcd.print(RTC.get(DS1307_DATE,false));//read date
  lcd.print("/");
  if ( RTC.get(DS1307_MTH,false) < 10 ){
    lcd.print("0");
  }
  lcd.print(RTC.get(DS1307_MTH,false));//read month
  lcd.print("/");
  lcd.print(RTC.get(DS1307_YR,false)); //read year 
}

void Clock::print_day(){
  strcpy_P(buffer, (char*)pgm_read_word(&(  days_names_table[  ( RTC.get(DS1307_DOW,false) -1 ) ]     )));
  lcd.print(buffer );
}

void Clock::set_time(int hr,int min,int sec){
  RTC.stop();
  RTC.set(DS1307_SEC,sec);        //set the seconds
  RTC.set(DS1307_MIN,min);     //set the minutes
  RTC.set(DS1307_HR,hr);       //set the hours (military)
  RTC.start();
}

void Clock::set_date(int yr,int mth,int date,int day){
  RTC.stop();
  RTC.set(DS1307_DOW,day);         //set the day of the week
  RTC.set(DS1307_DATE,date);   //set the date
  RTC.set(DS1307_MTH,mth);        //set the month
  RTC.set(DS1307_YR,(yr % 1000));              //set the year
  RTC.start();
}

/*  
 Serial.print(RTC.get(DS1307_HR,true)); //read the hour and also update all the values by pushing in true
 Serial.print(":");
 Serial.print(RTC.get(DS1307_MIN,false));//read minutes without update (false)
 Serial.print(":");
 Serial.print(RTC.get(DS1307_SEC,false));//read seconds
 Serial.print("      ");                 // some space for a more happy life
 Serial.print(RTC.get(DS1307_DATE,false));//read date
 Serial.print("/");
 Serial.print(RTC.get(DS1307_MTH,false));//read month
 Serial.print("/");
 Serial.print(RTC.get(DS1307_YR,false)); //read year 
 Serial.println();
 */



